package com.example.videowall;



import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.text.Layout;
import android.view.DragEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnClickListener;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.videowall.SlidingPanel;
import android.widget.Toast;
import android.content.Intent;


public class MainActivity extends Activity implements OnClickListener, OnTouchListener {

	AlertDialog.Builder setDialog;
	Context context;
	TextView tvName;
	Layout myl;
	private int oNumber;
    int key=0;
    float x;
    float y;
    String sDown;
    String sMove;
    String sUp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//findViewById(R.id.slidingpanel).setOnTouchListener(this);
		
		final SlidingPanel popup = (SlidingPanel) findViewById(R.id.sliding_layout);
        popup.setVisibility(View.GONE);
        
        final Button btn=(Button)findViewById(R.id.show1);
        btn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                        if(key==0){
                                key=1;
                                popup.setVisibility(View.VISIBLE);
                                btn.setBackgroundResource(R.drawable.next_item);
                        }
                        else if(key==1){
                                key=0;
                                popup.setVisibility(View.GONE);
                                btn.setBackgroundResource(R.drawable.next_item);
                        }
                }
        });
        
        
}
		
		
		
		 
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		//return MenuChoice(item);
		// Handle presses on the action bar items
	    switch (item.getItemId()) {
	        case R.id.apply:
	        	Toast toast = Toast.makeText(getApplicationContext(), 
	        	"VideoWall settings are applied!", Toast.LENGTH_SHORT); 
	        	toast.show(); 
	            return true;
	        case R.id.settings:
	        	
	        	Intent intent = new Intent(this, DialogActivity.class);
	    	    startActivityForResult(intent, 1);
	            return true;
	        case R.id.exit:
	        	finish();
	        	System.exit(0);
	            return true;
	        default:
	        	return MenuChoice(item);
	            
	    }
		
	}
	
	@Override
	  public void onClick(View v) {
	    Intent intent = new Intent(this, DialogActivity.class);
	    startActivityForResult(intent, 1);
	  }

	  
	  @Override
	  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (data == null) {return;}
	    int signals_number = data.getIntExtra("signals_number", 0);
	    addIcons(signals_number);
	    for (int imageId: imageIds) {
	    	ImageView images = (ImageView) findViewById(imageIds[imageId]);
			images.setOnTouchListener(new OnTouchListener()
			  {
			    int prevX,prevY;

			    @Override
			    public boolean onTouch(final View v,final MotionEvent event) {
			    	final LinearLayout.LayoutParams par=(LinearLayout.LayoutParams)v.getLayoutParams();
			        switch(event.getAction())
			          {
			          case MotionEvent.ACTION_MOVE:
			            {
			            par.topMargin+=(int)event.getRawY()-prevY;
			            prevY=(int)event.getRawY();
			            par.leftMargin+=(int)event.getRawX()-prevX;
			            prevX=(int)event.getRawX();
			            v.setLayoutParams(par);
			            return true;
			            }
			          case MotionEvent.ACTION_UP:
			            {
			            par.topMargin+=(int)event.getRawY()-prevY;
			            par.leftMargin+=(int)event.getRawX()-prevX;
			            v.setLayoutParams(par);
			            return true;
			            }
			          case MotionEvent.ACTION_DOWN:
			            {
			            prevX=(int)event.getRawX();
			            prevY=(int)event.getRawY();
			            par.bottomMargin=-2*v.getHeight();
			            par.rightMargin=-2*v.getWidth();
			            v.setLayoutParams(par);
			            return true;
			            }
			          }
			   return false; }
			  });
			}
	  }
	
	private boolean MenuChoice(MenuItem item) {
		// TODO Auto-generated method stub
		return false;
	}
	
	
	
	Button btn[] = new Button[oNumber];
	public void createButtons(int oNumber) {
		LinearLayout myLayout = (LinearLayout) findViewById(R.id.slidingpanel);
		
		for (int i=1; i<oNumber+1; i++) {
		     Button btn = new Button(this);
		     btn.setId(i);
		     btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
		     btn.setText("Screen" + i);
		    myLayout.addView(btn); 
		} }

	
	
	
	Integer[] imageIds = {
			R.drawable.arrow,
			R.drawable.prev_item,
			};
	
	public void addIcons(int signals_number) {
		LinearLayout myLayout = (LinearLayout) findViewById(R.id.slidingpanel);
		
		for (int imageId=0; imageId<signals_number; imageId++) {
			ImageView imageView = new ImageView(this);
			imageView.setImageResource(imageIds[imageId]);
		    imageView.setId(imageId);
		     imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
		    
		    myLayout.addView(imageView); 
		  

		} 
		
	}



	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return false;
	}
	}
	


	
